package com.pktechan.manage.service.impl;

import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.dao.SysRoleMapper;
import com.pktechan.manage.dao.SysUserMapper;
import com.pktechan.manage.dao.SysUserRoleMapMapper;
import com.pktechan.manage.entity.*;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.SysRoleService;
import com.pktechan.manage.service.SysUserService;
import com.pktechan.manage.utils.EncrptUtil;
import com.pktechan.manage.utils.StringUtil;
import com.pktechan.manage.vo.SysUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
    private static final Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapMapper sysUserRoleMapMapper;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<SysUser> getBaseMapper() {
        return sysUserMapper;
    }

    /**
     * 重置密码
     * @param user
     */
    @Override
    public void updateSystemUserPwd(SysUser user) {
        if (user.getId() == null) {
            logger.warn("修改密码失败，员工号不合法。");
            throw new CustomException("修改密码失败，员工号不合法。");
        }
        SysUser sysUser = (SysUser) sysUserMapper.getById(user.getId());

        if (sysUser != null) {
            // 存在。
            logger.debug("修改密码");
            user.setPassword(EncrptUtil.encryptMD5(user.getPassword()));
            sysUserMapper.update(user);
        } else {
            // 不存在。
            logger.warn("修改密码失败，该员工不存在。");
            throw new CustomException("修改密码失败，该员工不存在。");
        }
    }

    /**
     * 删除指定的用户信息。
     * @param userId
     */
    @Override
    public void deleteSystemUser(Integer userId) throws Exception{
        logger.debug("删除指定的员工信息");

        if (userId == null) {
            logger.warn("删除员工失败，员工号不合法。");
            throw new CustomException("删除员工失败，员工号不合法。");
        }
        SysUser user = (SysUser) sysUserMapper.getById(userId);

        if (user != null) {
            // 存在。
            sysUserMapper.deleteById(userId);
        } else {
            // 不存在。
            logger.warn("删除员工信息失败，该员工不存在。");
            throw new CustomException("删除员工信息失败，该员工不存在。");
        }
    }

    /**
     * 批量删除指定的用户信息。
     * @param user
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteUserAll(SysUserVO user, int id) throws Exception {
        String arr[] = user.getAllUserId().split(",");
        for (String allUserId : arr) {
            int userId = Integer.parseInt(allUserId);
            //判断登录用户与所选用户 ID是否一致 一致则不能删除
            if (id != userId) {
                sysRoleService.deleteAllRoleByUser(userId);
                deleteSystemUser(userId);
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据userId查询用户基本信息及权限
     *
     * @param sysUser 员工信息条件。
     */
    public Map getSystemUserList(SysUser sysUser) throws Exception {
        logger.debug("根据userId查询用户基本信息及权限");
        //用户基本信息
        List<SysUser> user = sysUserMapper.getList(sysUser);
        //拥有权限
        List<SysUserRoleMap> roleIds = sysUserRoleMapMapper.getList(sysUser.getId());
        SysRole sysRole = new SysRole();
        //所有权限
        List<SysRole> flag = sysRoleMapper.getList(sysRole);
        Map map = new HashMap();
        map.put("user", user);
        if (roleIds != null) {
            map.put("roleIds", roleIds);
        }
        map.put("flag", flag);
        return map;
    }

    /**
     * 修改指定的用户信息。
     * @param sysUser
     */
    @Override
    public void updateSystemUser(SysUserVO sysUser) {
        logger.debug("修改指定的员工信息");

        if (sysUser.getId() == null) {
            logger.debug("修改员工失败，员工号不合法。");
            throw new CustomException("修改员工失败，员工号不合法。");
        }
        SysUser user = (SysUser) sysUserMapper.getById(sysUser.getId());
        if (user != null) {
            if (!StringUtil.isEmpty(sysUser.getPassword())) {
                // 密码加密。
                sysUser.setPassword(EncrptUtil.encryptMD5(sysUser.getPassword()));
            }
            // 存在。
            sysUserMapper.update(sysUser);
        } else {
            // 不存在。
            logger.debug("修改员工信息失败，该员工不存在。");
            throw new CustomException("修改员工信息失败，该员工不存在。");
        }
    }
}
